<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use backend\models\PayLogistics;
use backend\models\PayScoreItem;
//处理来自其第三方网站的回调
class CallbackController extends Controller {
    //快递订阅回调函数
    public $enableCsrfValidation = false;
    public function actionKuaidi()
    {
        if (Yii::$app->request->post('param'))
        {
            $result=json_decode(Yii::$app->request->post('param'),true);
            if ($result['status']!='abort')
            {
                $logistics_name = key_exists('comOld', $result)&&$result['comOld']!=''?$result['comOld']:$result['lastResult']['com'];
                $logistics_trade=$result['lastResult']['nu'];
                $logistics_message=json_encode($result['lastResult']['data'],JSON_UNESCAPED_UNICODE);
                $res = PayLogistics::findOne(['logistics_trade'=>$logistics_trade,'logistics_name'=>$logistics_name]);
                if (!empty($res))
                {
                    if (key_exists('autoCheck', $result)&&$result['autoCheck']=='1')
                    {
                        $res->logistics_name=$result['comNew'];
                    }
                    $res->logistics_message=$logistics_message;
                    $res->save();
                }
            }
        }
    }
    /**
     * 金币兑换的快递
     */
    public function actionKuaidiscore()
    {
    	if (Yii::$app->request->post('param'))
    	{
    		$result=json_decode(Yii::$app->request->post('param'),true);
    		if ($result['status']!='abort')
    		{
    			$logistics_name = key_exists('comOld', $result)&&$result['comOld']!=''?$result['comOld']:$result['lastResult']['com'];
    			$logistics_trade=$result['lastResult']['nu'];
    			$logistics_message=json_encode($result['lastResult']['data'],JSON_UNESCAPED_UNICODE);
    			
    			$res = PayScoreItem::findOne(['logistics_trade'=>$logistics_trade,'logistics_name'=>$logistics_name]);

    			if (!empty($res))
    			{
    				if (key_exists('autoCheck', $result)&&$result['autoCheck']=='1')
    				{
    					$res->logistics_name=$result['comNew'];
    				}
    				$res->logistics_message=$logistics_message;
    				$res->save();
    			}
    		}
    	}
    }
}